package com.bajiao.notify;

import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.io.*;


/**
 * @Description: 移动生成文件到项目下
 * @Author: FanJq
 * @Date: 2024/10/10
 * @Version: V1.0
 */

@PropertySource({"jeecg_config.properties"})
@Component
public class MoveFile {

    //生成的包名
    private static String moveBackPackName = "" ;
    //代码生成位置
    private static String moveProjectPath;

    private static String moveBasePath;
    //业务包
    private static String moveBussiPackage;
    //根目录
    private static String moveSourceRootPackage;

    @Value("moveProjectPath")
    public static void setMoveProjectPath(String moveProjectPath) {
        MoveFile.moveProjectPath = moveProjectPath;
    }
    @Value("moveBussiPackage")
    public static void setMoveBussiPackage(String moveBussiPackage) {
        MoveFile.moveBussiPackage = moveBussiPackage;
    }
    @Value("moveSourceRootPackage")
    public static void setMoveSourceRootPackage(String moveSourceRootPackage) {
        MoveFile.moveSourceRootPackage = moveSourceRootPackage;
    }

    public static void main(String[] args) throws Exception {

        moveBasePath = System.getProperty("user.dir");

        String basePath = moveProjectPath + moveSourceRootPackage + moveBussiPackage + moveBackPackName; //D:\git_project\02-pth\pth_manager\bajiao-demo\src\main\java\com\bajiao\demo\

        String upperName = initialsToUpper(moveBackPackName);
        System.out.println(basePath);  //D:\git_project\02-pth\pth_manager\bajiao-demo\src\main\java\com\bajiao\demo\banner
        System.out.println(basePath + "\\entity\\" + upperName + ".java"); //D:\git_project\02-pth\pth_manager\bajiao-demo\src\main\java\com\bajiao\demo\banner\entity\Banner.java

        System.out.println("用户的当前工作目录:" + System.getProperty("user.dir"));


        //1.移动实体
        File fromFile1 = new File(basePath + "\\entity\\" + upperName + ".java");
        String toFile1Str = moveBasePath + "\\bajiao-entity\\src\\main\\java\\com\\bajiao\\entity\\" + moveBackPackName;
        File toFile1 = new File(toFile1Str);
        if (!toFile1.exists()) {
            toFile1.mkdir();
        }
        FileUtil.copyFile(fromFile1, toFile1);
        //替换字符
        setText(new File(toFile1Str + "\\" + upperName + ".java"), "com.bajiao.demo." + moveBackPackName + ".entity", "com.bajiao.entity." + moveBackPackName);


        //2.移动mapper.java
        File fromFile2 = new File(basePath + "\\mapper\\" + upperName + "Mapper.java");
        String toFile2Str = moveBasePath + "\\bajiao-mapper\\src\\main\\java\\com\\bajiao\\mapper\\" + moveBackPackName;
        File toFile2 = new File(toFile2Str);
        if (!toFile2.exists()) {
            toFile2.mkdir();
        }
        FileUtil.copyFile(fromFile2, toFile2);
        //替换字符
        setText(new File(toFile2Str + "\\" + upperName + "Mapper.java"), "com.bajiao.demo." + moveBackPackName + ".mapper", "com.bajiao.mapper." + moveBackPackName);
        setText(new File(toFile2Str + "\\" + upperName + "Mapper.java"), "com.bajiao.demo." + moveBackPackName + ".entity." + upperName, "com.bajiao.entity." + moveBackPackName + "." + upperName);


        //3.移动mapper.java
        File fromFile3 = new File(basePath + "\\mapper\\xml\\" + upperName + "Mapper.xml");
        String toFile3Str = moveBasePath + "\\bajiao-mapper\\src\\main\\java\\com\\bajiao\\mapper\\" + moveBackPackName + "\\xml";
        File toFile3 = new File(toFile3Str);
        if (!toFile3.exists()) {
            toFile3.mkdir();
        }
        FileUtil.copyFile(fromFile3, toFile3);
        //替换字符
        setText(new File(toFile3Str + "\\" + upperName + "Mapper.xml"), "com.bajiao.demo." + moveBackPackName + ".mapper." + upperName + "Mapper", "com.bajiao.mapper." + moveBackPackName + "." + upperName + "Mapper");


        //4.移动 service.java
        File fromFile4 = new File(basePath + "\\service\\I" + upperName + "ServiceAdmin.java");
        String toFile4Str = moveBasePath + "\\bajiao-service\\src\\main\\java\\com\\bajiao\\service\\admin\\" + moveBackPackName;
        File toFile4 = new File(toFile4Str);
        if (!toFile4.exists()) {
            toFile4.mkdir();
        }
        FileUtil.copyFile(fromFile4, toFile4);
        //替换字符
        setText(new File(toFile4Str + "\\I" + upperName + "ServiceAdmin.java"), "com.bajiao.demo." + moveBackPackName + ".service", "com.bajiao.service.admin." + moveBackPackName);
        setText(new File(toFile4Str + "\\I" + upperName + "ServiceAdmin.java"), "com.bajiao.demo." + moveBackPackName + ".entity." + upperName, "com.bajiao.entity." + moveBackPackName + "." + upperName);


        //5.移动 service.java
        File fromFile5 = new File(basePath + "\\service\\impl\\" + upperName + "ServiceAdminImpl.java");
        String toFile5Str = moveBasePath + "\\bajiao-service\\src\\main\\java\\com\\bajiao\\service\\admin\\" + moveBackPackName + "\\impl";
        File toFile5 = new File(toFile5Str);
        if (!toFile5.exists()) {
            toFile5.mkdir();
        }
        FileUtil.copyFile(fromFile5, toFile5);
        //替换字符
        setText(new File(toFile5Str + "\\" + upperName + "ServiceAdminImpl.java"), "com.bajiao.demo." + moveBackPackName + ".service.impl", "com.bajiao.service.admin." + moveBackPackName + ".impl");
        setText(new File(toFile5Str + "\\" + upperName + "ServiceAdminImpl.java"), "com.bajiao.demo." + moveBackPackName + ".entity." + upperName, "com.bajiao.entity." + moveBackPackName + "." + upperName);
        setText(new File(toFile5Str + "\\" + upperName + "ServiceAdminImpl.java"), "com.bajiao.demo." + moveBackPackName + ".mapper." + upperName + "Mapper", "com.bajiao.mapper." + moveBackPackName + "." + upperName + "Mapper");
        setText(new File(toFile5Str + "\\" + upperName + "ServiceAdminImpl.java"), "com.bajiao.demo." + moveBackPackName + ".service.I" + upperName + "ServiceAdmin", "com.bajiao.service.admin." + moveBackPackName + ".I" + upperName + "ServiceAdmin");


        //6.移动 service.java
        File fromFile6 = new File(basePath + "\\service\\I" + upperName + "ServiceMove.java");
        String toFile6Str = moveBasePath + "\\bajiao-service\\src\\main\\java\\com\\bajiao\\service\\app\\" + moveBackPackName;
        File toFile6 = new File(toFile6Str);
        if (!toFile6.exists()) {
            toFile6.mkdir();
        }
        FileUtil.copyFile(fromFile6, toFile6);
        //替换字符
        setText(new File(toFile6Str + "\\I" + upperName + "ServiceMove.java"), "com.bajiao.demo." + moveBackPackName + ".service", "com.bajiao.service.app." + moveBackPackName);
        setText(new File(toFile6Str + "\\I" + upperName + "ServiceMove.java"), "com.bajiao.demo." + moveBackPackName + ".entity." + upperName, "com.bajiao.entity." + moveBackPackName + "." + upperName);


        //7.移动 service.java
        File fromFile7 = new File(basePath + "\\service\\impl\\" + upperName + "ServiceMoveImpl.java");
        String toFile7Str = moveBasePath + "\\bajiao-service\\src\\main\\java\\com\\bajiao\\service\\app\\" + moveBackPackName + "\\impl";
        File toFile7 = new File(toFile7Str);
        if (!toFile7.exists()) {
            toFile7.mkdir();
        }
        FileUtil.copyFile(fromFile7, toFile7);
        //替换字符
        setText(new File(toFile7Str + "\\" + upperName + "ServiceMoveImpl.java"), "com.bajiao.demo." + moveBackPackName + ".service.impl", "com.bajiao.service.app." + moveBackPackName + ".impl");
        setText(new File(toFile7Str + "\\" + upperName + "ServiceMoveImpl.java"), "com.bajiao.demo." + moveBackPackName + ".mapper." + upperName + "Mapper", "com.bajiao.mapper." + moveBackPackName + "." + upperName + "Mapper");
        setText(new File(toFile7Str + "\\" + upperName + "ServiceMoveImpl.java"), "com.bajiao.demo." + moveBackPackName + ".service.I" + upperName + "ServiceMove", "com.bajiao.service.app." + moveBackPackName + ".I" + upperName + "ServiceMove");
        setText(new File(toFile7Str + "\\" + upperName + "ServiceMoveImpl.java"), "com.bajiao.demo." + moveBackPackName + ".entity." + upperName, "com.bajiao.entity." + moveBackPackName + "." + upperName);

        //8.移动controller
        File fromFile8 = new File(basePath + "\\controller\\" + upperName + "Controller.java");
        String toFile8Str = moveBasePath + "\\bajiao-api\\src\\main\\java\\com\\bajiao\\controller\\admin\\" + moveBackPackName;
        File toFile8 = new File(toFile8Str);
        if (!toFile8.exists()) {
            toFile8.mkdir();
        }
        FileUtil.copyFile(fromFile8, toFile8);

        //替换字符
        setText(new File(toFile8Str + "\\" + upperName + "Controller.java"), "com.bajiao.demo." + moveBackPackName + ".controller", "com.bajiao.controller.admin." + moveBackPackName);
        setText(new File(toFile8Str + "\\" + upperName + "Controller.java"), "com.bajiao.demo." + moveBackPackName + ".service.I" + upperName + "ServiceAdmin", "com.bajiao.service.admin." + moveBackPackName + ".I" + upperName + "ServiceAdmin");
        setText(new File(toFile8Str + "\\" + upperName + "Controller.java"), "com.bajiao.demo." + moveBackPackName + ".entity." + upperName, "com.bajiao.entity." + moveBackPackName + "." + upperName);


        //9.移动controller
        File fromFile9 = new File(basePath + "\\controller\\Rest" + upperName + "Controller.java");
        String toFile9Str = moveBasePath + "\\bajiao-api\\src\\main\\java\\com\\bajiao\\controller\\app\\" + moveBackPackName;
        File toFile9 = new File(toFile9Str);
        if (!toFile9.exists()) {
            toFile9.mkdir();
        }
        FileUtil.copyFile(fromFile9, toFile9);
        //替换字符
        setText(new File(toFile9Str + "\\Rest" + upperName + "Controller.java"), "com.bajiao.demo." + moveBackPackName + ".controller", "com.bajiao.controller.app." + moveBackPackName);
        setText(new File(toFile9Str + "\\Rest" + upperName + "Controller.java"), "com.bajiao.demo." + moveBackPackName + ".service.I" + upperName + "ServiceMove", "com.bajiao.service.app." + moveBackPackName + ".I" + upperName + "ServiceMove");
        setText(new File(toFile9Str + "\\Rest" + upperName + "Controller.java"), "com.bajiao.demo." + moveBackPackName + ".entity." + upperName, "com.bajiao.entity." + moveBackPackName + "." + upperName);


        //删除demo下边的文件
        delFolder(basePath + "\\controller");
        delFolder(basePath + "\\entity");
        delFolder(basePath + "\\mapper");
        delFolder(basePath + "\\service");
    }


    /**
     * @description: 单词首字母大写
     * @author: FanJq
     * @date: 2024-10-10
     * @return:
     */
    private static String initialsToUpper(String word) {
        word = word.substring(0, 1).toUpperCase() + word.substring(1);
        return word;
    }


    private static void setText(File fileText, String target, String src) {//修改

        BufferedReader br = null;
        PrintWriter pw = null;
        StringBuffer buff = new StringBuffer();//临时容器!
        String line = System.getProperty("line.separator");//平台换行!
        try {
            br = new BufferedReader(new FileReader(fileText));
            for (String str = br.readLine(); str != null; str = br.readLine()) {
                if (str.contains(target)) {
                    str = str.replaceAll(target, src);
                }
                buff.append(str + line);
            }
            pw = new PrintWriter(new FileWriter(fileText), true);
            pw.println(buff);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (pw != null) {
                    pw.close();
                }
            }
        }
    }

    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);//再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }

    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); //删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            File myFilePath = new File(filePath);
            myFilePath.delete(); //删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
